package com.geek.factory.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.entity.Productx;
import com.geek.factory.result.Result;
import com.geek.factory.service.ProductxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/productx")
@Api(tags = "产品管理")
@Slf4j
public class ProductxController {

 @Autowired
 private ProductxService productxService;


    /**
     * 新增产品
     */
    @PostMapping("/create")
    @ApiOperation("新增产品")
    public Result createProduct(@RequestBody Productx productx) {
        log.info("新增产品, 参数: {}", productx);
        try {
            productxService.save(productx);
            return new Result(SuccessConstant.SUCCESS, "新增成功");
        } catch (Exception e) {
            log.error("新增产品失败", e);
            return new Result(SuccessConstant.FAIL, "新增失败");
        }
    }

    /**
     * 根据ID查询产品
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询产品")
    public Result getProductById(@PathVariable Integer id) {
        log.info("根据ID查询产品, id: {}", id);
        try {
            Productx productx = productxService.getById(id);
            return new Result(SuccessConstant.SUCCESS, "查询成功", productx);
        } catch (Exception e) {
            log.error("查询产品失败", e);
            return new Result(SuccessConstant.FAIL, "查询失败");
        }
    }

    /**
     * 修改产品
     */
    @PutMapping("/update")
    @ApiOperation("修改产品")
    public Result updateProduct(@RequestBody Productx productx) {
        log.info("修改产品, 参数: {}", productx);
        try {
            productxService.updateById(productx);
            return new Result(SuccessConstant.SUCCESS, "修改成功");
        } catch (Exception e) {
            log.error("修改产品失败", e);
            return new Result(SuccessConstant.FAIL, "修改失败");
        }
    }

    /**
     * 根据ID删除产品
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据ID删除产品")
    public Result deleteProduct(@PathVariable Integer id) {
        log.info("根据ID删除产品, id: {}", id);
        try {
            productxService.removeById(id);
            return new Result(SuccessConstant.SUCCESS, "删除成功");
        } catch (Exception e) {
            log.error("删除产品失败", e);
            return new Result(SuccessConstant.FAIL, "删除失败");
        }
    }

    @PostMapping("/list")
    @ApiOperation("分页查询产品列表")
    public Result getProductList(@RequestBody(required = false) Map<String, Object> params) {

        log.info("分页查询产品列表 - 参数: {}", params);

        try {
            // 1. 获取分页参数
            Integer current = params != null && params.get("current") != null ?
                    Integer.valueOf(params.get("current").toString()) : 1;
            Integer size = params != null && params.get("size") != null ?
                    Integer.valueOf(params.get("size").toString()) : 10;

            // 2. 构建分页对象
            Page<Productx> page = new Page<>(current, size);

            // 3. 构建查询条件
            LambdaQueryWrapper<Productx> wrapper = new LambdaQueryWrapper<>();
            if (params != null) {
                // 按产品名称模糊查询
                if (params.get("name") != null && StringUtils.isNotBlank(params.get("name").toString())) {
                    wrapper.like(Productx::getName, params.get("name").toString());
                }
                // 按产品编码模糊查询
                if (params.get("productCode") != null && StringUtils.isNotBlank(params.get("productCode").toString())) {
                    wrapper.like(Productx::getId, params.get("id").toString());
                }
                // 按产品型号模糊查询
                if (params.get("model") != null && StringUtils.isNotBlank(params.get("model").toString())) {
                    wrapper.like(Productx::getModel, params.get("model").toString());
                }
            }

            // 4. 执行分页查询
            Page<Productx> resultPage = productxService.page(page, wrapper);

            // 5. 构造返回结果
            Map<String, Object> result = new HashMap<>();
            result.put("records", resultPage.getRecords());    // 数据列表
            result.put("total", resultPage.getTotal());        // 总记录数
            result.put("current", resultPage.getCurrent());    // 当前页码
            result.put("size", resultPage.getSize());          // 每页大小

            return new Result(SuccessConstant.SUCCESS, "查询成功", result);

        } catch (Exception e) {
            log.error("查询产品列表失败", e);
            return new Result(SuccessConstant.FAIL, "查询失败");
        }
    }
}
